package util;

import Bean.FileBean;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by eleven on 15/12/30.
 */
@Service(value = "allUtil")
public class AllUtil {



    private final static String filepath = "/Users/eleven/out/create/";

    private final static String readpath = "/Users/eleven/workplace/data-center/dao/src/main/java/com/datatrees/data/domain/";

    private final static String controllpath = "/Users/eleven/workplace/data-center/web/src/main/java/com/datatrees/data/controller/report/";


    /**
     * 创建文件
     * @return
     */
    public Boolean createFile(FileBean fileBean){

        try {
            if(StringUtil.isNotEmpty(fileBean.getFilename())&&StringUtil.isNotEmpty(fileBean.getFiletype())) {
                String fileAddress = filepath + fileBean.getFilename() + "." + fileBean.getFiletype();
                File file = new File(fileAddress);
                if (file.length()==0)
                    file.createNewFile();
                return true;
            }else{
                return false;
            }
        }catch (Exception e){
            return false;
        }
    }

    /**
     * 直接写文件
     * @param text
     * @return
     */
    public Boolean writeFile(FileBean fileBean,String text){
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(fileBean.getFilepath()));
            out.write(text); // \r\n即为换行
            out.flush(); // 把缓存区内容压入文件
            out.close();
            return true;
        }catch (Exception e){
            return false;
        }

    }

    /**
     * 直接写文件
     * @param
     * @return
     */
    public Boolean writeObjectFile(Object tableData){
        try {
            File file = new File("C:/Users/SunZZ/Desktop/data.txt");
            FileOutputStream out=new FileOutputStream(file,true);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(out);
//            for(Object[] o:tableData) {
            objectOutputStream.writeObject(tableData);
//            }
//            out.flush();
            out.close();
            return true;
        }catch (Exception e){
            return false;
        }

    }

    /**
     * 读取dao文件返回get函数名
     * @param way
     * @return
     */
    public List<String> readDaoFile(String way){
        try {
//            String path = readpath+way;
            InputStreamReader reader = new InputStreamReader(
                    new FileInputStream(way));
            BufferedReader br = new BufferedReader(reader);
            String line = "";
            line = br.readLine();
            List<String> ss=new ArrayList();
            while (line != null) {
                String[] s = line.split(" ");
                for (String t:s){
                    if (t.indexOf("get")!=-1){
                       ss.add(t);
                    }
                }
                line = br.readLine(); // 一次读入一行数据
            }
            br.close();
            return ss;
        }catch (Exception e){
            System.out.println("出错了"+e);
            return new ArrayList<>();
        }

    }

    /**
     * 在controller文件里添加新方法
     * @param way
     * @param text
     * @return
     */
    public Boolean writeInSameFile(String way, String text){
        try {
            int totallines = this.getTotalLines(way);
//            String path = controllpath+way;
            InputStreamReader reader = new InputStreamReader(
                    new FileInputStream(way));
            BufferedReader br = new BufferedReader(reader);
            String write = "";
            String line = "";
            line = br.readLine();
            if (line!=null){
                write = write+line+"\n";
            }
            String ss = "";
           for (int i=1;i<totallines-1;i++) {
                ss=ss+line;
                line = br.readLine();// 一次读入一行数据
               if (line!=null){
                   write = write+line+"\n";
               }
            }
            write = write+text;
            line = br.readLine();
            if (line!=null){
                write = write+line;
            }
            BufferedWriter out = new BufferedWriter(new FileWriter(way));
            out.write(write);
            br.close();
            out.flush();
            out.close();
            reader.close();
            return true;
        }catch (Exception e){
            System.out.println("出错了"+e);
            return false;
        }

    }

    /**
     * 在文件的某一行添加新的内容(service的需求)
     * @param pathWay
     * @param text
     * @return
     */
    public Boolean writeInFileWhichLine(int count, String pathWay, String text){
        try {
            int totallines = this.getTotalLines(pathWay);
//            String path = controllpath+pathWay;
            if (count>totallines){
                    return false;
            }else {
                InputStreamReader reader = new InputStreamReader(
                        new FileInputStream(pathWay));
                BufferedReader br = new BufferedReader(reader);
                String write = "";
                String line = "";
                line = br.readLine();
                if (line != null) {
                    write = write + line + "\n";
                }
                String ss = "";
                for (int i = 1; i < count; i++) {
                    ss = ss + line;
                    line = br.readLine();// 一次读入一行数据
                    if (line != null) {
                        write = write + line + "\n";
                    }
                }
                write = write + text;
                line = br.readLine();
                while (line != null) {
                    write = write + line;
                    line = br.readLine();// 一次读入一行数据
                }
                BufferedWriter out = new BufferedWriter(new FileWriter(pathWay));
                out.write(write);
                br.close();
                out.flush();
                out.close();
                reader.close();
                return true;
            }
        }catch (Exception e){
            System.out.println("出错了"+e);
            return false;
        }

    }
    /**
     * 获得文件的行数
     * @param way
     * @return
     * @throws IOException
     */
    public int getTotalLines(String way) throws IOException{
//        String path = controllpath+way;
        InputStreamReader reader = new InputStreamReader(
                new FileInputStream(way));
        BufferedReader br = new BufferedReader(reader);
        String line = br.readLine();
        int count=0;
        while (line != null) {
            count++;
            line = br.readLine();// 一次读入一行数据
        }
        br.close();
        reader.close();
        return count;
    }

    /**
     * 遍历文件夹
     * @param path
     * @return
     */
    public List<String> traverseFolder(String path) {
        List<String> folderpath = new ArrayList<>();
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (files.length == 0) {
//                System.out.println("文件夹是空的!");
                return null;
            } else {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        System.out.println("文件夹:" + file2.getAbsolutePath());
                        folderpath.add(file2.getAbsolutePath());
//                        String[] s = file2.getAbsolutePath().split("/");
//                        System.out.println("文件夹最后名字:" + s[10]);
//                        traverseFolder2(file2.getAbsolutePath());
                    }
//                    else {
//                        System.out.println("文件:" + file2.getAbsolutePath());
//                    }
                }
                return folderpath;
            }
        } else {
            System.out.println("文件不存在!");
            return null;
        }
    }
}
